package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.DocMedicineEntity;
import com.ywjh.inter.entity.UserMedPlanEntity;
import com.ywjh.inter.entity.UserMedPlanMedEntity;
import com.ywjh.inter.service.MedPlanService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MedPlan extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5547396400497800866L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		System.out.println(postData.toString());
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "add":
			int period = data.getInt("period");
			String period_unit = data.getString("period_unit");
			String week_day = data.getString("week_day");
			Timestamp start_time = StrUtil.getTimestamp(data.getString("start_time"));
			Timestamp add_time = new Timestamp(System.currentTimeMillis());
			int user_id = data.getInt("user_id");
			int doc_id = data.getInt("doc_id");
			JSONArray medArray = data.getJSONArray("med_array");
			int status = 1;
			UserMedPlanEntity entity = new UserMedPlanEntity();
			entity.setAdd_time(add_time);
			entity.setDoc_id(doc_id);
			entity.setPeriod(period);
			entity.setPeriod_unit(period_unit);
			entity.setStart_time(start_time);
			entity.setAdd_time(add_time);
			entity.setStatus(status);
			entity.setUser_id(user_id);
			entity.setWeek_day(week_day);
			int id = MedPlanService.getInstance().addUserMedPlan(entity, medArray);
			if (id > -1) {
				r = 1;
			} else {
				e = "系统错误";
			}
			break;
		case "update":
			id = data.getInt("id");
			period = data.getInt("period");
			period_unit = data.getString("period_unit");
			week_day = data.getString("week_day");
			start_time = StrUtil.getTimestamp(data.getString("start_time"));
			add_time = new Timestamp(System.currentTimeMillis());
			user_id = data.getInt("user_id");
			doc_id = data.getInt("doc_id");
			medArray = data.getJSONArray("med_array");
			entity = MedPlanService.getInstance().findMedPlanById(id);
			if (entity == null) {
				e = "未找到药品计划";
			} else {
				entity.setAdd_time(add_time);
				entity.setDoc_id(doc_id);
				entity.setPeriod(period);
				entity.setPeriod_unit(period_unit);
				entity.setStart_time(start_time);
				entity.setAdd_time(add_time);
				entity.setUser_id(user_id);
				entity.setWeek_day(week_day);
				if (MedPlanService.getInstance().updateUserMedPlan(entity, medArray)) {
					r = 1;
				} else {
					e = "系统错误";
				}
			}
			break;
		case "delete":
			id = data.getInt("id");
			user_id = data.getInt("user_id");
			entity = MedPlanService.getInstance().findMedPlanById(id);
			if (entity == null) {
				e = "未找到药品计划";
			} else {
				entity.setStatus(0);
				MedPlanService.getInstance().deleteUserMedPlan(entity);
				r = 1;
			}
			break;
		case "find_by_id":
			id = data.getInt("id");
			entity = MedPlanService.getInstance().findMedPlanById(id);
			if (entity == null) {
				e = "未找到药品计划";
			} else {
				r = 1;
				JSONObject d = new JSONObject();
				d.put("id", entity.getId());
				d.put("user_id", entity.getUser_id());
				d.put("doc_id", entity.getDoc_id());
				d.put("week_day", entity.getWeek_day());
				d.put("period", entity.getPeriod());
				d.put("period_unit", entity.getPeriod_unit());
				d.put("start_time", StrUtil.getYYYYMMDD(entity.getStart_time()));
				d.put("end_time", StrUtil.getYYYYMMDD(entity.getEnd_time()));
				d.put("add_time", StrUtil.getYYYYMMDDHHMISS(entity.getAdd_time()));
				List<UserMedPlanMedEntity> list = MedPlanService.getInstance().listMedByPlanId(entity.getId());
				JSONArray med_array = new JSONArray();
				long zuijin = Long.MAX_VALUE;
				for (UserMedPlanMedEntity userMedPlanMedEntity : list) {
					DocMedicineEntity medEntity = MedPlanService.getInstance()
							.findMedById(userMedPlanMedEntity.getMed_id());
					if (medEntity != null) {
						JSONObject med = new JSONObject();
						med.put("med_id", entity.getId());
						med.put("med_name",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getName());
						med.put("med_unit",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getUnit());
						med.put("med_usage", MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id())
								.getDrugusage());
						med.put("med_count", userMedPlanMedEntity.getCount());
						med.put("med_time", userMedPlanMedEntity.getTime());
						long next_time = MedPlanService.getInstance().nextTime(entity, userMedPlanMedEntity.getTime());
						med.put("next_time", next_time);
						med.put("next_time_str", MedPlanService.getInstance().formatDateTime(next_time / 1000));
						med_array.add(med);
						d.put("med_id", userMedPlanMedEntity.getMed_id());
						d.put("med_name",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getName());
						if (zuijin > next_time) {
							zuijin = next_time;
							d.put("med_count", userMedPlanMedEntity.getCount());
							d.put("next_time", zuijin);
							d.put("next_time_str", MedPlanService.getInstance().formatDateTime(next_time / 1000));
						}
					}
				}
				d.put("med_array", med_array);
				json.put("d", d);
			}
			break;
		case "list_by_user_id":
			user_id = data.getInt("user_id");
			JSONArray array = new JSONArray();
			List<UserMedPlanEntity> list = MedPlanService.getInstance().findMedPlanByUserId(user_id);
			for (UserMedPlanEntity en : list) {
				JSONObject d = new JSONObject();
				d.put("id", en.getId());
				d.put("user_id", en.getUser_id());
				d.put("doc_id", en.getDoc_id());
				d.put("week_day", en.getWeek_day());
				d.put("period", en.getPeriod());
				d.put("period_unit", en.getPeriod_unit());
				d.put("start_time", StrUtil.getYYYYMMDD(en.getStart_time()));
				d.put("end_time", StrUtil.getYYYYMMDD(en.getEnd_time()));
				d.put("add_time", StrUtil.getYYYYMMDDHHMISS(en.getAdd_time()));
				List<UserMedPlanMedEntity> planMedList = MedPlanService.getInstance().listMedByPlanId(en.getId());
				JSONArray med_array = new JSONArray();
				long zuijin = Long.MAX_VALUE;
				for (UserMedPlanMedEntity userMedPlanMedEntity : planMedList) {
					DocMedicineEntity medEntity = MedPlanService.getInstance()
							.findMedById(userMedPlanMedEntity.getMed_id());
					if (medEntity != null) {
						JSONObject med = new JSONObject();
						med.put("med_id", userMedPlanMedEntity.getMed_id());
						med.put("med_name",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getName());
						med.put("med_unit",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getUnit());
						med.put("med_usage", MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id())
								.getDrugusage());
						med.put("med_count", userMedPlanMedEntity.getCount());
						med.put("med_time", userMedPlanMedEntity.getTime());
						long next_time = MedPlanService.getInstance().nextTime(en, userMedPlanMedEntity.getTime());
						med.put("next_time", next_time);
						med.put("next_time_str", MedPlanService.getInstance().formatDateTime(next_time / 1000));
						med_array.add(med);
						d.put("med_id", userMedPlanMedEntity.getMed_id());
						d.put("med_name",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getName());
						if (zuijin > next_time) {
							zuijin = next_time;
							d.put("med_count", userMedPlanMedEntity.getCount());
							d.put("next_time", zuijin);
							d.put("next_time_str", MedPlanService.getInstance().formatDateTime(next_time / 1000));
						}
					}
				}
				d.put("med_array", med_array);
				array.add(d);
			}
			r = 1;
			json.put("d", array);
			break;
		case "list_by_doc_id":
			doc_id = data.getInt("doc_id");
			JSONArray array1 = new JSONArray();
			List<UserMedPlanEntity> list1 = MedPlanService.getInstance().findMedPlanByDocId(doc_id);
			for (UserMedPlanEntity en : list1) {
				JSONObject d = new JSONObject();
				d.put("id", en.getId());
				d.put("user_id", en.getUser_id());
				d.put("doc_id", en.getDoc_id());
				d.put("week_day", en.getWeek_day());
				d.put("period", en.getPeriod());
				d.put("period_unit", en.getPeriod_unit());
				d.put("start_time", StrUtil.getYYYYMMDD(en.getStart_time()));
				d.put("end_time", StrUtil.getYYYYMMDD(en.getEnd_time()));
				d.put("add_time", StrUtil.getYYYYMMDDHHMISS(en.getAdd_time()));
				List<UserMedPlanMedEntity> planMedList = MedPlanService.getInstance().listMedByPlanId(en.getId());
				JSONArray med_array = new JSONArray();
				long zuijin = Long.MAX_VALUE;
				for (UserMedPlanMedEntity userMedPlanMedEntity : planMedList) {
					DocMedicineEntity medEntity = MedPlanService.getInstance()
							.findMedById(userMedPlanMedEntity.getMed_id());
					if (medEntity != null) {
						JSONObject med = new JSONObject();
						med.put("med_id", userMedPlanMedEntity.getMed_id());
						med.put("med_name",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getName());
						med.put("med_unit",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getUnit());
						med.put("med_usage", MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id())
								.getDrugusage());
						med.put("med_count", userMedPlanMedEntity.getCount());
						med.put("med_time", userMedPlanMedEntity.getTime());
						long next_time = MedPlanService.getInstance().nextTime(en, userMedPlanMedEntity.getTime());
						med.put("next_time", next_time);
						med.put("next_time_str", MedPlanService.getInstance().formatDateTime(next_time / 1000));
						med_array.add(med);
						d.put("med_id", userMedPlanMedEntity.getMed_id());
						d.put("med_name",
								MedPlanService.getInstance().findMedById(userMedPlanMedEntity.getMed_id()).getName());
						if (zuijin > next_time) {
							zuijin = next_time;
							d.put("med_count", userMedPlanMedEntity.getCount());
							d.put("next_time", zuijin);
							d.put("next_time_str", MedPlanService.getInstance().formatDateTime(next_time / 1000));
						}
					}
				}
				d.put("med_array", med_array);
				array1.add(d);
			}
			r = 1;
			json.put("d", array1);
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
